class Admin::CategoryDescriptionsController < Admin
  # GET /category_descriptions
  # GET /category_descriptions.xml
  def index
    @category_descriptions = CategoryDescription.find(:all)

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @category_descriptions }
    end
  end

  # GET /category_descriptions/1
  # GET /category_descriptions/1.xml
  def show
    @category_description = CategoryDescription.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @category_description }
    end
  end

  # GET /category_descriptions/new
  # GET /category_descriptions/new.xml
  def new
    @category_description = CategoryDescription.new
    @category = Category.find(params[:category_id])

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @category_description }
    end
  end

  # GET /category_descriptions/1/edit
  def edit
    @category_description = CategoryDescription.find(params[:id])
  end

  # POST /category_descriptions
  # POST /category_descriptions.xml
  def create
    @category_description = CategoryDescription.new(params[:category_description])
    category = Category.find(@category_description.category_id)
    category.last_edited_by = current_user
    category.save
    
    respond_to do |format|
      if @category_description.save
        flash[:notice] = 'category description(s) successfully created.'
        format.html { redirect_to(:back) }
        format.xml  { render :xml => @category_description, :status => :created, :location => @category_description }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @category_description.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /category_descriptions/1
  # PUT /category_descriptions/1.xml
  def update
    @category_description = CategoryDescription.find(params[:id])
    category = Category.find(@category_description.category_id)
    category.last_edited_by = current_user
    category.save
    
    old_language, old_description = @category_description.language, @category_description.description

    respond_to do |format|
      if @category_description.update_attributes(params[:category_description])
        flash[:notice] = 'categoryDescription was successfully updated.'
        format.html { redirect_to(@category_description) }
        format.xml  { head :ok }
        format.js { render :update do |page|
        if old_language != @category_description.language
            page.visual_effect :highlight, 'language'
        end
        if old_description != @category_description.description
          page.visual_effect :highlight, 'description'
        end
   
      end}
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @category_description.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /category_descriptions/1
  # DELETE /category_descriptions/1.xml
  def destroy
    @category_description = CategoryDescription.find(params[:id])
    @category_description.destroy
    @category_description.category.unpublish_if_necessary
    
    category = Category.find(@category_description.category_id)
    category.last_edited_by = current_user
    category.save

    respond_to do |format|
      format.html { redirect_to(categories_url) }
      format.xml  { head :ok }
      format.js { render :update do |page|
        page.remove "category_description_#{params[:id]}_row"
        page.visual_effect :highlight, 'category_description_table', :duration => 2
      end }
    end
  end
  
  def add_ajax
    @category_description = CategoryDescription.new(params[:category_description])
    @category_description.category_id = params[:category_id]
    respond_to do |format|
      if @category_description.save
        format.js { render :update do |page|
          page.insert_html :bottom, "category_description_table", {:partial => "admin/category_descriptions/edit_description", :object => @category_description}
          page.visual_effect :highlight, "category_description_table", :duration => 2
          page[:category_description_description].value = ''
          page[:category_description_language].value = ''
        end }

      else
        #error
        format.js { render :text => 'error'}
      end
    end
  end
  
  def update_ajax
    @category_description = CategoryDescription.find(params[:id])
    respond_to do |format|
      if @category_description.update_attributes(params[:category_description])
        format.js { render :update do |page|
          page.visual_effect :highlight, 'description_' + @category_description.id.to_s
        end }
      else
        # TODO: error
        format.js { render :update do |page|
          
        end }
      end
    end
  end
end
